package com.example.api.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public final class DateUtils {

    /**
     * 当前年
     * @return
     */
    public static int getYear() {
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        return c.get(Calendar.YEAR);
    }

    /**
     * 当前月
     * @return
     */
    public static int getMonth(){
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        return c.get(Calendar.MONTH) + 1;
    }

    /**
     * 当前天
     * @return
     */
    public static int getDay(){
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        return c.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取指定日期 Date --> String
     * @param date
     * @param style
     * @return
     */
    public static String dateToString(Date date, String style) {
        SimpleDateFormat df = new SimpleDateFormat(style);
        return df.format(date);
    }

    /**
     * 获取指定日期 String --> Date
     * @param strDate
     * @param style
     * @return
     */
    public static Date stringToDate(String strDate, String style) {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(style);
        try {
            date = df.parse(strDate);
        } catch (ParseException e) {
            date = DateUtils.stringToDate(strDate, "yyyyMMdd");
        }
        return date;
    }

}
